package engine.gui;


import java.awt.Font;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.Color;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

import engine.entities.AbstractEntity;

public class Button extends AbstractEntity 
{
	private boolean isEnabled=true, isTop=false, AlignCenter=false;
	
	private String text;
	private UnicodeFont font;//font -> simple font, font_up - mouse is on the top
	private Color color,color_top;
	
	@SuppressWarnings("unchecked")
	public Button(double x, double y, Font awtFont, String text, Color color, Color top_color)
	{
		super(x, y, 0, 0);
		this.text=text;
		this.color=color;
		this.color_top=top_color;
		font = new UnicodeFont(awtFont);
		font.getEffects().add(new ColorEffect(java.awt.Color.white));
		font.addAsciiGlyphs();
	    try
	    {
	    	font.loadGlyphs();
	    } catch (SlickException e)
	    {
	    	e.printStackTrace();
	    }
	    setWidth(font.getWidth(text));
	    setHeight(font.getLineHeight());
	}
	
	@Override
	public void render()
	{
		if(!isEnabled)
			return;
		float x=0,y=0;
		if(AlignCenter)
		{
			x=(float)this.x-font.getWidth(text)/2;
			y=(float)this.y-font.getHeight(text)/2;
		}
		else
		{
			x=(float)this.x;
			y=(float)this.y;
		}
		if(!isTop)
			font.drawString(x, y, text, color);
		else
			font.drawString(x, y, text, color_top);
	}
	
	@Override
	public void update(int delta)
	{
		double x=0,y=0;
		if(AlignCenter)
		{
			x=this.x-font.getWidth(text)/2;
			y=this.y-font.getHeight(text)/2;
		}
		else
		{
			x=this.x;
			y=this.y;
		}
		if(!isEnabled)
			return;
		if( x<Mouse.getX() && x+width>Mouse.getX() && y<Display.getHeight()-Mouse.getY() && y+height>Display.getHeight()-Mouse.getY() )
			isTop=true;
		else isTop=false;
	}
	
	public String text()
	{
		return text;
	}
	
	public void setText(String text)
	{
		this.text=text;
		setWidth(font.getWidth(text));
	    setHeight(font.getLineHeight());
	}
	public boolean isTop()
	{
		return isTop;
	}
	public boolean isEnabled()
	{
		return isEnabled;
	}
	public void setEnabled(boolean isEnabled)
	{
		this.isEnabled=isEnabled;
	}
	public void setAlignCenter(boolean isCenter)
	{
		AlignCenter=isCenter;
	}
	
}